﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krile.Kernel.Data;
using Krile.Kernel;
using Krile.Kernel.WindowsEx;

namespace Twinkle.Tweak
{
    public class TReplyData : ReplyData
    {
        public TReplyData(string id, string replyToUserId)
            : base(id, replyToUserId) { }

        public override string ReplyToText
        {
            get
            {
                try
                {
                    var uid = Kernel.Storage.GetUid(base.ReplyToId);
                    if (uid != 0)
                        return Bridge.StatusManager.GetStatus(uid).Text.Original;
                    else
                        return string.Empty;
                }
                catch (NullReferenceException)
                {
                    return string.Empty;
                }
            }
            set { }
        }

        public override string ReplyToUserName
        {
            get
            {
                try
                {
                    var uid = Kernel.Storage.GetUid(base.ReplyToId);
                    if (uid != 0)
                    {
                        var sts = Bridge.StatusManager.GetStatus(uid);
                        if (sts == null || sts.User == null) return String.Empty;
                        if (Kernel.Config.StatusDesign.ShowIDAlternative)
                            return "@" + this.ReplyToUserId + " / " + sts.User.Name;
                        else
                            return sts.User.Name;
                    }
                    else
                        return string.Empty;
                }
                catch (Exception e)
                {
                    Bridge.DebugReporter.AddReport("Invalid replydata is registered! " +
                        " id:" + this.ReplyToId +
                        " text:" + this.ReplyToText +
                        " uid:" + this.ReplyToUserId +
                        " uname:" + this.ReplyToUserName + Environment.NewLine +
                        "Exception is:" + e.ToString());
                    return string.Empty;
                }
            }
            set { }
        }

        public override ulong ReplyOnClick()
        {
            var uid = Kernel.Storage.GetUid(ReplyToId);
            if (uid != 0)
                return uid;
            else
            {
                if (MessageBoxEx.Show(
                    Lang.Msg.ReplyJump_Context, Lang.Msg.ReplyJump, Lang.Msg.ReplyJump_Title,
                    MessageBoxEx.MessageBoxExButtons.YesNo, MessageBoxEx.MessageBoxExIcon.Information)
                    == MessageBoxEx.DialogResultEx.Yes)
                {
                    if (ReplyToId == null)
                        Bridge.CommonOperation.CallBrowser("http://twitter.com/" + ReplyToUserId);
                    else
                        Bridge.CommonOperation.CallBrowser("http://twitter.com/" + ReplyToUserId + "/status/" + ReplyToId);
                }
                return 0;
            }
        }

    }
}
